/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ttfsclient;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import pojo.POJOFileClient;
import pojo.POJOInfoClient;
import pojo.POJOInfoSharedFile;
import pojo.POJOMyInfo;
import myutil.Protocol;

/**
 *
 * @author Tidus Le
 */
public class clsRequestSV
{
    /**
     * Gửi thông tin định danh lên server
     */
    public static void dinhDanh(POJOMyInfo myinfo,String ip)
    {
        boolean response=false;
        try
        {
            Protocol prot=new Protocol();
            if(myinfo.getId().isEmpty()==true)
            {
                prot.setDk(true);
                prot.setData(InetAddress.getLocalHost().getHostName().getBytes());
                response=true;
            }
            else
            {
                prot.setOn(true);
                prot.setData(myinfo.getId().getBytes());
            }
            
            prot.setId(ip);
            prot.setStt(Integer.parseInt(myinfo.getPort()));
            
            Socket sock=null;
            try
            {
                sock=new Socket(myinfo.getSvIP(), Integer.parseInt(myinfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            if(response==true)
            {
                //receive response
                InputStream inStr=sock.getInputStream();
                ObjectInputStream objInStr=new ObjectInputStream(inStr);
                prot=(Protocol)objInStr.readObject();
                frmMain.myInfo.setId(new String(prot.getData()));
                frmMain.changeInfo=true;
                inStr.close();
                objInStr.close();
            }
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Gửi thông tin offline lên server
     */
    public static void offline(POJOMyInfo myinfo)
    {
        try
        {   
            Protocol prot=new Protocol();
            prot.setOff(true);
            prot.setData(myinfo.getId().getBytes());
            
            Socket sock=null;
            try
            {
                sock=new Socket(myinfo.getSvIP(), Integer.parseInt(myinfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Gửi yêu cầu share file đến server
     */
    public static void shareFile(String fid,String name,long len,POJOMyInfo myinfo)
    {
        try
        {   
            Protocol prot=new Protocol();
            prot.setRs(true);
            prot.setId(myinfo.getId());
            prot.setData(name.getBytes());
            prot.setLen(len);
            prot.setFid(fid);
            
            Socket sock=null;
            try
            {
                sock=new Socket(myinfo.getSvIP(), Integer.parseInt(myinfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Lấy danh sách các file được chia sẻ trên server
     */
    public static void getLlistSharedFile(POJOMyInfo myinfo)
    {
        try
        {
            Protocol prot=new Protocol();
            prot.setRl(true);
            
            Socket sock=null;
            try
            {
                sock=new Socket(myinfo.getSvIP(), Integer.parseInt(myinfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            //receive response
            InputStream inStr=sock.getInputStream();
            ObjectInputStream objInStr=new ObjectInputStream(inStr);
            
            frmMain.arrInfoSharedFiles=(ArrayList<POJOInfoSharedFile>)objInStr.readObject(); //Nhận shared file list
            frmMain.arrFileClient=(ArrayList<POJOFileClient>)objInStr.readObject(); //Nhận file-client
            frmMain.arrInfoClient=(ArrayList<POJOInfoClient>)objInStr.readObject(); //Nhận thông tin về client
            
            //notify to frmMain
            frmMain.changeSharedList=true;
            
            //close
            inStr.close();
            objInStr.close();
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Gửi yêu cầu xóa file đã chia sẻ
     */
    public static void deleteSharedFile(POJOMyInfo myinfo,String fid)
    {
        try
        {
            Protocol prot=new Protocol();
            prot.setRd(true);
            prot.setFid(fid);
            prot.setId(myinfo.getId());
            
            Socket sock=null;
            try
            {
                sock=new Socket(myinfo.getSvIP(), Integer.parseInt(myinfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * Lấy địa chỉ IP mới nhất của client
     */
    public static POJOInfoClient getLocation(POJOMyInfo myInfo,String cid)
    {
        POJOInfoClient info=new POJOInfoClient();
        try
        {
            Protocol prot=new Protocol();
            prot.setGl(true);
            prot.setId(cid);
            
            Socket sock=null;
            try
            {
                sock=new Socket(myInfo.getSvIP(), Integer.parseInt(myInfo.getSvPort()));
                frmMain.connected=true;
            }catch(Exception ex)
            {
                frmMain.connected=false;
                JOptionPane.showMessageDialog(new JFrame(), "Không thể kết nối đến server !","Lỗi !",JOptionPane.ERROR_MESSAGE);
                return info;
            }
            
            //send request
            OutputStream outStr=sock.getOutputStream();
            ObjectOutputStream objOutStr=new ObjectOutputStream(outStr);
            objOutStr.writeObject(prot);
            objOutStr.flush();
            
            //receive response
            InputStream inStr=sock.getInputStream();
            ObjectInputStream objInStr=new ObjectInputStream(inStr);
            prot=(Protocol)objInStr.readObject();
            
            info.setId(cid);
            info.setIp(prot.getId());
            info.setPort(prot.getFid());
            
            //close
            inStr.close();
            objInStr.close();
            
            objOutStr.close();
            outStr.close();
            sock.close();
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Lỗi !",JOptionPane.ERROR_MESSAGE);
        }finally
        {
            return info;
        }
    }
}
